package com.wangjj.basic;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class EchoServer {

	/**
	 * Simple Echo Server
	 */
	public static void main(String[] args){
		Socket s = null;
		try {
			ServerSocket server = new ServerSocket(8500);
			s = server.accept();
			InputStream inputStream = s.getInputStream();
			OutputStream outputStream = s.getOutputStream();
			Scanner input = new Scanner(inputStream);
			PrintWriter out = new PrintWriter(outputStream,true);
			
			out.println("Hello! Please Enter Bye to Exit");
			
			boolean done = false;
			
			while(!done && input.hasNextLine()) {
				String line = input.nextLine();
				out.println("Echo:" + line);
				if(line.equals("Bye")) {
					done = true;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
